cls

@echo off
COLOR 0A
@echo *
@echo * Microsoft Azure Mobile Service (Telemetry) Exerciser

call env.bat

@echo * Setting Mobil eService Table and Record GUID.
@echo * Edit in telemetry.bat

set MSTable=telemetry
set RecordGUID=%1
@echo ON

@echo * 
@echo * Table = %MSTable%
@echo * Record GUID (for Delete and Update) = %RecordGUID%
@echo *

@echo * And now for some CRUD
@echo * Create Read Update Delete

@echo *
pause
cls
@echo off
COLOR 07
@echo on
@echo *
@echo * [0] GET (Select/Read) all records in table
@echo * > GET /tables/%MSTable%
@echo *
curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  "%AzureMobileServiceURL%/tables/%MSTable%" 
@echo *
pause

cls
@echo *
@echo * [1] POST (Insert/Create) a new record
@echo *
curl -v -X POST -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey% -d "{\"sensor\":\"temperature\",\"date\":\"29-01-2015\",\"value\": 131}" %AzureMobileServiceURL%/tables/%MSTable%
pause

@echo *
@echo *     Get all records in table (again)
curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  "%AzureMobileServiceURL%/tables/%MSTable%" 
@echo *
pause

cls
@echo *
@echo * [2.1] GET Filtered on a numeric value-1 EQ
@echo *
curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  "%AzureMobileServiceURL%/tables/%MSTable%?$filter=(value%%20eq%%20131)" 

@echo *
pause

cls
@echo *
@echo * [2.2] GET Filtered on a numeric value-2 GT
@echo *
curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  "%AzureMobileServiceURL%/tables/%MSTable%?$filter=(value%%20gt%%2060)" 
@echo *
pause

cls
@echo *
@echo * [2.3] GET Filtered on a string value startswith
@echo *
curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  "%AzureMobileServiceURL%/tables/%MSTable%?$filter=(startswith(sensor,'Temperature'))" 
@echo *
pause

COLOR 0A
cls
@echo *
@echo * HINT: When using %% in batch file (as in filter) use %%%% [eg. Instead of (value%%20eq%%20131) use (value%%%%20eq%%%%20131) ]
@echo *
pause

COLOR 07

:CHECK

@echo off
if NOT ASD%1ASD==ASDASD GOTO DoPatchnDelete

cls
COLOR 74
@echo *
@echo * Skipping Update and Delete
@echo *
@echo * Update (Patch) and Delete require a record id (as a GUID .. without braces) in the command line
@echo *
pause
COLOR 07

GOTO Done



:DoPatchnDelete

cls
@echo on
@echo *
@echo * [3.1 PATCH  (Update, Note PATCH, Note not PUT)
@echo * > PATCH /tables/%MSTable%/%GUD%
@echo *
curl -v  -X PATCH -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  -d "{\"sensor\":\"salinity\",\"complete\":\"true\",\"value\": 200}" %AzureMobileServiceURL%/tables/%MSTable%/%RecordGUID% 
@echo *
pause


cls
@echo *
@echo * [3.2] PATCH Only need fields to be updated to have specified values (other fields remain unchanged).
@echo * > PATCH /tables/%MSTable%/%GUD%
@echo *
curl -v  -X PATCH -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  -d "{\"value\": 333}" %AzureMobileServiceURL%/tables/%MSTable%/%RecordGUID%
@echo *
pause


cls
@echo *
@echo * [3.3] PATCH: Can't use filters .. Must use the id with update:
@echo * > PATCH /tables/%MSTable%/$filter=(value%%20eq%%201004)
@echo *
curl -v  -X PATCH -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  -d "{\"value\": 333}" "%AzureMobileServiceURL%/tables/%MSTable%/$filter=(value%%20eq%%201004)"
@echo * {"code":400,"error":"Error: An id value must be specified in the URL for an update operation."}* Connection #0 to host sportronicsdj.azure-mobile.net left intact
@echo * {"code":404,"error":"Error: An item with id '$filter=(value eq 1004)' does not exist." Connection #0 to host sportronicsdj.azure-mobile.net left intact
@echo *
pause

cls
@echo *
@echo * [4.1] DELETE
curl -v -X DELETE -H Content-Type:application/json -H X-ZUMO-APPLICATION:NtcMLPQtuAqWtvXOwrZVQtqHevNUnN27  %AzureMobileServiceURL%/tables/%MSTable%/%RecordGUID% 
@echo *
@echo *     Get all reords in table (again)
curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey%  "%AzureMobileServiceURL%/tables/%MSTable%"
@echo *
pause

cls
@echo *
@echo * [4.2] DELETE: Can't use filters ... Must use id:
@echo * > HTTP 1.1 405 Method Not Allowed
@echo *
curl -v -X DELETE -H Content-Type:application/json -H X-ZUMO-APPLICATION:NtcMLPQtuAqWtvXOwrZVQtqHevNUnN27  "%AzureMobileServiceURL%/tables/%MSTable%?$filter=(value%20eq%201004)" 

@echo * DONE
COLOR 07
 GOTO Done



:Done

